package com.example.administrator.szb.fragments.fragment_forTab;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.SearchActivity;
import com.example.administrator.szb.adapter.ViewPageAdapter;
import com.example.administrator.szb.fragments.base.BaseFragment;
import com.example.administrator.szb.bean.BusinessBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.URLAddress;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/9/5.
 */

public class FragmentMessage extends BaseFragment {
    @Bind(R.id.wd_search)
    ImageView wdSearch;
    @Bind(R.id.wd_fb)
    ImageView wdFb;
    @Bind(R.id.wd_tablayout)
    TabLayout wdTablayout;
    @Bind(R.id.wd_viewPage)
    ViewPager wdViewPage;
    @Bind(R.id.wd_view_fb)
    View wdViewFb;
    private View view;
    BusinessBean.DataBean dataBean = new BusinessBean.DataBean();
    ArrayList<BusinessBean.DataBean> business = new ArrayList<>();//业务标签

    private callbackValue Interface_callback;//回调接口，用来和activity互通信息
    public static BusinessBean businessBean;

    View error_net;//网络异常显示

    /**
     * 回调接口，用来传记数值给目标activity,经过中介（父类activity）再向目标值传递
     *
     * @author asus
     */
    public interface callbackValue {
        public void sendDataOfRecord(ArrayList<BusinessBean.DataBean> business, int kinds);
    }
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Interface_callback = (callbackValue) context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(false);
        dataBean.setId(0);
        dataBean.setTitle("全部");
        business.add(dataBean);
       // SampleApplicationLike.showXFC();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_message, container, false);
        error_net = view.findViewById(R.id.message_error_net);
        ButterKnife.bind(this, view);
        requestBusiness();//请求业务标签
        return view;
    }

    /**
     * 请求业务标签
     */
    private void requestBusiness() {
        HashMap<String, String> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("business_id", "0");
        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, BusinessBean.class, URLAddress.URLBASE + URLAddress.URL_Business, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                businessBean = (BusinessBean) response;
                if (businessBean.getResult() == 1) {
                    business.addAll((ArrayList<BusinessBean.DataBean>) businessBean.getData());
                    //new FragmentWDType().initView();
                    initTabLayout();
                    wdTablayout.setVisibility(View.VISIBLE);
                    error_net.setVisibility(View.GONE);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                wdTablayout.setVisibility(View.GONE);
                error_net.setVisibility(View.VISIBLE);

                error_net.findViewById(R.id.home_fragment_button_djcs_error).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        requestBusiness();
                    }
                });
            }
        });
    }

    /**
     * 完成请求后，初始化tablayout
     */
    private void initTabLayout() {
        for (int i = 0; i < business.size(); i++) {
            wdTablayout.addTab(wdTablayout.newTab().setText(business.get(i).getTitle()));
        }

        initViewPager();//初始化viewpager
    }

    private void initViewPager() {
        wdViewPage.setOffscreenPageLimit(1);
        wdViewPage.setAdapter(new ViewPageAdapter(getChildFragmentManager(), business));
        wdTablayout.setupWithViewPager(wdViewPage);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.wd_search, R.id.wd_fb,R.id.wd_view_fb})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.wd_search:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
            case R.id.wd_fb:
                Interface_callback.sendDataOfRecord(business, 0);//通过接口向askActivity传值，这里的0没什么意义，以后可做为其他参数
                break;
            case R.id.wd_view_fb:
                Interface_callback.sendDataOfRecord(business, 0);//增加点击区域
                break;
        }
    }
}
